//
//  ApplicationCompletionViewController.m
//  SaltLightDemo
//
//  Created by lzy on 2018/5/8.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "ApplicationCompletionViewController.h"
#define TIMECOUNT 3600
@interface ApplicationCompletionViewController ()
@property (nonatomic , strong) UIButton *backBtn;
@property (assign, nonatomic) int count;
@end

@implementation ApplicationCompletionViewController
- (IBAction)call:(id)sender {
    [Util callAtOnce:@"400-700-1996"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.count = TIMECOUNT;
    self.title = @"申请完成";
    
    self.hideDefaultBackButton = YES;
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backBtn = button1;
    [button1 setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    button1.frame = CGRectMake(17, 0, 50, 44);
    [button1 addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    button1.titleLabel.font = [UIFont systemFontOfSize:16];
    [button1 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [button1 setTitle:@"" forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:button1];
    
    [self performSelectorInBackground:@selector(timerThread) withObject:nil];
    // Do any additional setup after loading the view from its nib.
}
- (void)timerThread {
    for (int i = TIMECOUNT; i >= 0 ; i--) {
        self.count--;
        //切换到主线程中更新UI
        [self performSelectorOnMainThread:@selector(updateFirstBtn) withObject:nil waitUntilDone:YES];
        sleep(1);
    }
}

//更新UI
- (void)updateFirstBtn {
    NSString *str = nil;
    if (self.count == 0) {
        str = [NSString stringWithFormat:@"00:00"];
        
    } else {
        str = [NSString stringWithFormat:@"%d:%d",self.count/60,self.count%60];
        
    }
    self.time.text = str;
}

-(void)goBack
{
    
   [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    self.backBtn.hidden = YES;
}
-(void)dealloc
{
    self.backBtn.hidden = YES;
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.backBtn.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
